﻿using System;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using SharpDX.XAudio2;
using SynergyLib.Wave;

namespace XAudio2Controls
{
    /// <summary>
    /// Provides Control for DrumPatchTabPanels.
    /// </summary>
    public class DrumPatchControl : XA2PatchBase
    {
        
        #region Singleton Patterns
        private static DrumPatchControl instance;
        public static DrumPatchControl Instance
        {
            get
            {
                if (instance == null)
                    instance = new DrumPatchControl();
                return instance;
            }
        }
        private static SpectrumAnalyzer dynamicspectrumanalyzer; 
        public static SpectrumAnalyzer DynamicSpectrumAnalyzer
        {
            get
            {
                if (dynamicspectrumanalyzer == null)
                    dynamicspectrumanalyzer = GetASpectrumAnalyzer();
                return dynamicspectrumanalyzer;
            }
            set { return; }
        }          
        #endregion Singleton Patterns
        #region Properties
        public GraphingEffect MasterGrapher
        { get; set; }
        internal static CurrentlySelectedTab CurrentTab { get; set; }
        #endregion Properties   
        #region Constructors
        static DrumPatchControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DrumPatchControl), new FrameworkPropertyMetadata(typeof(XA2PatchBase)));
        }
        /// <summary>
        /// (for now)
        /// ShouldGraphChangedEventHandler needs to exist or FFT graphing breaks
        /// </summary>
        private DrumPatchControl()
        {
            CurrentTab = CurrentlySelectedTab.Patch;
            MasterGrapher = new GraphingEffect();
            EffectDescriptor e = new EffectDescriptor(MasterGrapher);
            ScheduledXAudio2.Engine.MasterAccessor.SetEffectChain(e);
        }        
        #endregion Constructors

        #region Event Overrides
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();           
            GetTemplateChildren();            
        }        
        #endregion Event Overrides

        #region Public Methods        
        public override string ToString()
        {
            return "Drum Machine";
        }
        #endregion Public Methods

        #region Private Methods
        private static SpectrumAnalyzer GetASpectrumAnalyzer()
        {
            return new SpectrumAnalyzer()
            {
                FFTComplexity = FFTDataSize.FFT2048,
                BarCount = 128,
                BarSpacing = .5,
                PeakFallDelay = 10,
                MinHeight = 100,
                Background = System.Windows.Media.Brushes.Black,
            };
        }       
        #endregion Private Methods

        #region Event Handlers
        /// <summary>
        /// Initializes an DrumPatchTabPanel control.  The actual DrumPatchHost
        /// does not exist until DrumPatchTabPanel makes it so.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void ButtonNewPatch_Click(object sender, RoutedEventArgs e)
        {            
            if (ListBoxPatches.ItemsSource == null)
                ListBoxPatches.ItemsSource = Patches;
            ListBoxPatches.ItemStringFormat = ToString();
            XAudio2Controls.XA2TabPanelBase newDrumPatch = new XAudio2Controls.DrumPatchTabPanel();
            
            newDrumPatch.PropertyChanged += newDrumPatch_PropertyChanged;            
            newDrumPatch.SelectedTabChanged += newDrumPatch_SelectedTabChanged;
            Patches.Add(newDrumPatch);
        }
        /// <summary>
        /// for keeping the selected tab in sync among all tab panel controls
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void newDrumPatch_SelectedTabChanged(object sender, RoutedEventArgs e)
        {
            if (Patches != null)
            {
                foreach (XA2TabPanelBase tpb in Patches)
                {
                    if (tpb.Tabs != null)
                    {
                        foreach (TabItem ti in tpb.Tabs)
                        {
                            if ((DrumPatchControl.CurrentlySelectedTab)ti.Tag == CurrentTab)
                            {
                                ti.IsSelected = true;
                            }
                            else
                            {
                                ti.IsSelected = false;
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Raises a property change notification that tells tab panels to add another rhythmbuttonbar
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
       
        /// <summary>
        /// Update the displayed name in the listboxpatches.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
        void newDrumPatch_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "FilePath":
                    ListBoxPatches.ItemStringFormat = null;
                    ListBoxPatches.ItemStringFormat = XAudio2Controls.DrumPatchTabPanel.NameProperty.ToString();
                    break;
            }
        }      
        #endregion Event Handlers
    }
}
